package com.jasonchoi.redisqueue.config;


import com.jasonchoi.redisqueue.pubsub.pub.CustomRedisSender;
import com.jasonchoi.redisqueue.pubsub.sub.CustomRedisReceiver;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

import static com.jasonchoi.redisqueue.pubsub.pub.CustomRedisSender.CHANNEL_RECEIVETEST;
import static com.jasonchoi.redisqueue.pubsub.sub.CustomRedisReceiver.RECEIVE_TEST;


/**
 * redis 消息队列-发布订阅(此种方式不支持持久化 建议使用 RedisQueueUtil)
 * @Author: JasonChoi
 * @Date: 2020/3/13 15:30
 */
@Configuration
@AutoConfigureAfter({CustomRedisSender.class})
public class RedisMqConfig {

    /**
     * 注入消息监听适配器
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(CustomRedisReceiver receiver){
        return new MessageListenerAdapter(receiver, RECEIVE_TEST);
    }
//    @Bean
//    public MessageListenerAdapter listenerAdapter2(CustomRedisReceiver receiver){
//        return new MessageListenerAdapter(receiver, RECEIVE_TEST2);
//    }

    /**
     * 注入消息监听容器
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(
            RedisConnectionFactory redisConnectionFactory,
            MessageListenerAdapter listenerAdapter, MessageListenerAdapter listenerAdapter2) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();

        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(listenerAdapter, new PatternTopic(CHANNEL_RECEIVETEST));
        //redisMessageListenerContainer.addMessageListener(listenerAdapter2, new PatternTopic(CHANNEL_RECEIVE_TEST2));

        return redisMessageListenerContainer;
    }
}
